package com.peng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.dto.SetmealDto;
import com.peng.entity.Category;
import com.peng.entity.Setmeal;
import com.peng.entity.SetmealDish;
import com.peng.service.CategoryService;
import com.peng.service.SetmealDishService;
import com.peng.service.SetmealService;
import com.peng.mapper.SetmealMapper;
import javafx.scene.chart.CategoryAxis;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author dell
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2022-05-22 20:33:11
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> pageR(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        baseMapper.selectPage(setmealPage,queryWrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            Long categoryId = record.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            setmealDto.setCategoryName(byId.getName());
            setmealDtos.add(setmealDto);
        }
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        setmealDtoPage.setRecords(setmealDtos);
        return setmealDtoPage;
    }
}




